package com.czhi.disruptor;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;

import java.nio.ByteBuffer;

public class OrderEventMain {

    public static void main(String[] args) {

        EventFactory<OrderEvent> eventFactory = new OrderEventFactory();
        int ringBufferSize = 1024 * 1024;
        WaitStrategy waitStrategy = new BlockingWaitStrategy();
        Disruptor<OrderEvent> disruptor = new Disruptor(eventFactory,ringBufferSize, DaemonThreadFactory.INSTANCE,ProducerType.SINGLE,waitStrategy);
        final RingBuffer ringBuffer = disruptor.getRingBuffer();
        final OrderEventProducer orderEventProducer = new OrderEventProducer(ringBuffer);
        disruptor.handleEventsWith(new OrderEventHandler());
        disruptor.start();
        final ByteBuffer byteBuffer = ByteBuffer.allocate(8);

        for (int i = 0; i < 100; i++) {
            byteBuffer.putLong(0, i);
            orderEventProducer.sendData(byteBuffer);
        }
        disruptor.shutdown();
    }
}
